#!/usr/bin/env node

//导入inquirer模块
const inquirer = require('inquirer')
//导入path模块
const path = require('path')
//导入fs模块
const fs = require('fs')
//导入ejs模块
const ejs = require('ejs')
inquirer.prompt([
    {
        type:'input',
        name:'name',
        message:'Project name?'
    },
    {
        type:'list',
        name:'type',
        message:'Project Type?',
        choices:['H5','App','Applets']
    }
]).then(anwsers => {
    console.log(anwsers)
    //模板目录
    const H5 = path.join(__dirname,'H5')
    const App = path.join(__dirname,'App')
    const Applets = path.join(__dirname,'Applets')
    //目标目录
    const destDir = process.cwd()

    //将模板下的文件全部转移到目标目录
    fs.readdir(anwsers.type,(err,files) => {
        if(err) throw err
        files.forEach(file => {
            //通过模板引擎渲染文件
            ejs.renderFile(path.join(anwsers.type,file),anwsers, (err,result) =>{
                if(err) throw err
                //将结果写入目标文件路径
                fs.writeFileSync(path.join(destDir,file),result)
            })
        })
    })
})